package com.guodu.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.CliqueRedDao;
import com.guodu.entity.CliqueRed;
import com.guodu.entity.CliqueRedStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.vo.CliqueRedQueryVo;

@Repository("cliqueRedDao")
public class CliqueRedDaoImpl extends BaseDao implements CliqueRedDao{

	
	public int findCount(CliqueRedQueryVo cliqueRedQueryVo,Units units,UserInfo userInfo) {
		StringBuffer hql = new StringBuffer("select count(*) from  CliqueRed c where 0=0");
		 if(cliqueRedQueryVo != null){
	        	if(cliqueRedQueryVo.getYearStart() != null && !cliqueRedQueryVo.getYearStart().trim().equals("")){
					hql.append(" and cliqueredYear >="+cliqueRedQueryVo.getYearStart());
				}
				if(cliqueRedQueryVo.getYearEnd() != null && !cliqueRedQueryVo.getYearEnd().trim().equals("")){
					hql.append(" and cliqueredYear <="+cliqueRedQueryVo.getYearEnd());
				}
				if(cliqueRedQueryVo.getSupply() != null && !cliqueRedQueryVo.getSupply().trim().equals("")){
					hql.append(" and c.cliqueredSupply like '%"+cliqueRedQueryVo.getSupply()+"%'");
				}
				if(cliqueRedQueryVo.getIndustry() != null && !cliqueRedQueryVo.getIndustry().trim().equals("")){
					hql.append(" and c.cliqueredIndustry like '%"+cliqueRedQueryVo.getIndustry()+"%'");
				}
				if(cliqueRedQueryVo.getSupplierName() != null && !cliqueRedQueryVo.getSupplierName().trim().equals("")){
					hql.append(" and c.supplierName like '%"+cliqueRedQueryVo.getSupplierName()+"%'");
				}
				if(cliqueRedQueryVo.getSupplierCode() != null && !cliqueRedQueryVo.getSupplierCode().trim().equals("")){
					hql.append(" and c.supplierCode like '%"+cliqueRedQueryVo.getSupplierCode()+"%'");
				}
				if(cliqueRedQueryVo.getCreateTimeStart() != null && !cliqueRedQueryVo.getCreateTimeStart().trim().equals("")){
					cliqueRedQueryVo.setCreateTimeStart(cliqueRedQueryVo.getCreateTimeStart());
					hql.append(" and c.createTime >= TO_DATE('"+cliqueRedQueryVo.getCreateTimeStart()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
				}
				if(cliqueRedQueryVo.getCreateTimeEnd() != null && !cliqueRedQueryVo.getCreateTimeEnd().trim().equals("")){
					cliqueRedQueryVo.setCreateTimeEnd(cliqueRedQueryVo.getCreateTimeEnd());
					hql.append(" and c.createTime <= TO_DATE('"+cliqueRedQueryVo.getCreateTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
				}
				if(cliqueRedQueryVo.getStatus()!=null&&cliqueRedQueryVo.getStatus().trim().length()>0&&!cliqueRedQueryVo.getStatus().equals("10")){
					hql.append(" and status ="+cliqueRedQueryVo.getStatus());
				}
				if(cliqueRedQueryVo.getProducts() != null && cliqueRedQueryVo.getProducts().trim().length() > 0){
					hql.append(" and c.products like '%"+cliqueRedQueryVo.getProducts()+"%'");					
				}
			}
				if(units==null){
					hql.append(" and (status=1 or (exists(select 1 from UserInfo u where u.userId=c.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
			}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
				hql.append(" and createPeople = "+userInfo.getUserId());
			}
		return super.findInt(hql.toString());
	}

	
	public List<CliqueRed> findAll(CliqueRedQueryVo cliqueRedQueryVo,Units units,UserInfo userInfo,int pageSize,int curPage) {
		StringBuffer hql = new StringBuffer("from CliqueRed c where 0=0");
        if(cliqueRedQueryVo != null){
        	if(cliqueRedQueryVo.getYearStart() != null && !cliqueRedQueryVo.getYearStart().trim().equals("")){
				hql.append(" and cliqueredYear >="+cliqueRedQueryVo.getYearStart());
			}
			if(cliqueRedQueryVo.getYearEnd() != null && !cliqueRedQueryVo.getYearEnd().trim().equals("")){
				hql.append(" and cliqueredYear <="+cliqueRedQueryVo.getYearEnd());
			}
			if(cliqueRedQueryVo.getSupply() != null && !cliqueRedQueryVo.getSupply().trim().equals("")){
				hql.append(" and c.cliqueredSupply like '%"+cliqueRedQueryVo.getSupply()+"%'");
			}
			if(cliqueRedQueryVo.getIndustry() != null && !cliqueRedQueryVo.getIndustry().trim().equals("")){
				hql.append(" and c.cliqueredIndustry like '%"+cliqueRedQueryVo.getIndustry()+"%'");
			}
			if(cliqueRedQueryVo.getSupplierName() != null && !cliqueRedQueryVo.getSupplierName().trim().equals("")){
				hql.append(" and c.supplierName like '%"+cliqueRedQueryVo.getSupplierName()+"%'");
			}
			if(cliqueRedQueryVo.getSupplierCode() != null && !cliqueRedQueryVo.getSupplierCode().trim().equals("")){
				hql.append(" and c.supplierCode like '%"+cliqueRedQueryVo.getSupplierCode()+"%'");
			}
			if(cliqueRedQueryVo.getCreateTimeStart() != null && !cliqueRedQueryVo.getCreateTimeStart().trim().equals("")){
				cliqueRedQueryVo.setCreateTimeStart(cliqueRedQueryVo.getCreateTimeStart());
				hql.append(" and c.createTime >= TO_DATE('"+cliqueRedQueryVo.getCreateTimeStart()+"  00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(cliqueRedQueryVo.getCreateTimeEnd() != null && !cliqueRedQueryVo.getCreateTimeEnd().trim().equals("")){
				cliqueRedQueryVo.setCreateTimeEnd(cliqueRedQueryVo.getCreateTimeEnd());
				hql.append(" and c.createTime <= TO_DATE('"+cliqueRedQueryVo.getCreateTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(cliqueRedQueryVo.getStatus()!=null&&cliqueRedQueryVo.getStatus().trim().length()>0&&!cliqueRedQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+cliqueRedQueryVo.getStatus());
			}
			if(cliqueRedQueryVo.getProducts() != null && cliqueRedQueryVo.getProducts().trim().length() > 0){
				hql.append(" and c.products like '%"+cliqueRedQueryVo.getProducts()+"%'");					
			}
		}	
        if(units==null){
			hql.append(" and (status=1 or (exists(select 1 from UserInfo u where u.userId=c.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}
	    hql.append("order by createTime desc");
		return super.findPages(hql.toString(),curPage,pageSize);
	}
    @SuppressWarnings("unchecked")
	public List<CliqueRed> downLoad(CliqueRedQueryVo cliqueRedQueryVo,Units units,UserInfo userInfo) {
    	StringBuffer hql = new StringBuffer("from CliqueRed c where 0=0");
        if(cliqueRedQueryVo != null){
        	if(cliqueRedQueryVo.getYearStart() != null && !cliqueRedQueryVo.getYearStart().trim().equals("")){
				hql.append(" and cliqueredYear >="+cliqueRedQueryVo.getYearStart());
			}
			if(cliqueRedQueryVo.getYearEnd() != null && !cliqueRedQueryVo.getYearEnd().trim().equals("")){
				hql.append(" and cliqueredYear <="+cliqueRedQueryVo.getYearEnd());
			}
			if(cliqueRedQueryVo.getSupply() != null && !cliqueRedQueryVo.getSupply().trim().equals("")){
				hql.append(" and c.cliqueredSupply like '%"+cliqueRedQueryVo.getSupply()+"%'");
			}
			if(cliqueRedQueryVo.getIndustry() != null && !cliqueRedQueryVo.getIndustry().trim().equals("")){
				hql.append(" and c.cliqueredIndustry like '%"+cliqueRedQueryVo.getIndustry()+"%'");
			}
			if(cliqueRedQueryVo.getSupplierName() != null && !cliqueRedQueryVo.getSupplierName().trim().equals("")){
				hql.append(" and c.supplierName like '%"+cliqueRedQueryVo.getSupplierName()+"%'");
			}
			if(cliqueRedQueryVo.getSupplierCode() != null && !cliqueRedQueryVo.getSupplierCode().trim().equals("")){
				hql.append(" and c.supplierCode like '%"+cliqueRedQueryVo.getSupplierCode()+"%'");
			}
			if(cliqueRedQueryVo.getCreateTimeStart() != null && !cliqueRedQueryVo.getCreateTimeStart().trim().equals("")){
				cliqueRedQueryVo.setCreateTimeStart(cliqueRedQueryVo.getCreateTimeStart());
				hql.append(" and c.createTime >= TO_DATE('"+cliqueRedQueryVo.getCreateTimeStart()+"  00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(cliqueRedQueryVo.getCreateTimeEnd() != null && !cliqueRedQueryVo.getCreateTimeEnd().trim().equals("")){
				cliqueRedQueryVo.setCreateTimeEnd(cliqueRedQueryVo.getCreateTimeEnd());
				hql.append(" and c.createTime <= TO_DATE('"+cliqueRedQueryVo.getCreateTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(cliqueRedQueryVo.getStatus()!=null&&cliqueRedQueryVo.getStatus().trim().length()>0&&!cliqueRedQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+cliqueRedQueryVo.getStatus());
			}
			if(cliqueRedQueryVo.getProducts() != null && cliqueRedQueryVo.getProducts().trim().length() > 0){
				hql.append(" and c.products like '%"+cliqueRedQueryVo.getProducts()+"%'");					
			}
		}
        if(units==null){
			hql.append(" and (status=1 or (exists(select 1 from UserInfo u where u.userId=c.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}
	    hql.append("order by createTime desc");
		return super.find(hql.toString());
	}
    
	public void examinAll(CliqueRedQueryVo cliqueRedQueryVo, Units units,
			UserInfo userInfo) {
		StringBuffer hql = new StringBuffer();
		if(units==null){
			hql.append("update from CliqueRed c set c.status=3 where c.status=0 ");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1L){
			hql.append("update from CliqueRed c set c.status=1 where c.status=0 ");
		}
		 if(cliqueRedQueryVo != null){
	        	if(cliqueRedQueryVo.getYearStart() != null && !cliqueRedQueryVo.getYearStart().trim().equals("")){
					hql.append(" and cliqueredYear >="+cliqueRedQueryVo.getYearStart());
				}
				if(cliqueRedQueryVo.getYearEnd() != null && !cliqueRedQueryVo.getYearEnd().trim().equals("")){
					hql.append(" and cliqueredYear <="+cliqueRedQueryVo.getYearEnd());
				}
				if(cliqueRedQueryVo.getSupply() != null && !cliqueRedQueryVo.getSupply().trim().equals("")){
					hql.append(" and c.cliqueredSupply like '%"+cliqueRedQueryVo.getSupply()+"%'");
				}
				if(cliqueRedQueryVo.getIndustry() != null && !cliqueRedQueryVo.getIndustry().trim().equals("")){
					hql.append(" and c.cliqueredIndustry like '%"+cliqueRedQueryVo.getIndustry()+"%'");
				}
				if(cliqueRedQueryVo.getSupplierName() != null && !cliqueRedQueryVo.getSupplierName().trim().equals("")){
					hql.append(" and c.supplierName like '%"+cliqueRedQueryVo.getSupplierName()+"%'");
				}
				if(cliqueRedQueryVo.getSupplierCode() != null && !cliqueRedQueryVo.getSupplierCode().trim().equals("")){
					hql.append(" and c.supplierCode like '%"+cliqueRedQueryVo.getSupplierCode()+"%'");
				}
				if(cliqueRedQueryVo.getCreateTimeStart() != null && !cliqueRedQueryVo.getCreateTimeStart().trim().equals("")){
					cliqueRedQueryVo.setCreateTimeStart(cliqueRedQueryVo.getCreateTimeStart());
					hql.append(" and c.createTime >= TO_DATE('"+cliqueRedQueryVo.getCreateTimeStart()+"  00:00:00','yyyy-mm-dd hh24:mi:ss')");
				}
				if(cliqueRedQueryVo.getCreateTimeEnd() != null && !cliqueRedQueryVo.getCreateTimeEnd().trim().equals("")){
					cliqueRedQueryVo.setCreateTimeEnd(cliqueRedQueryVo.getCreateTimeEnd());
					hql.append(" and c.createTime <= TO_DATE('"+cliqueRedQueryVo.getCreateTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
				}
				if(cliqueRedQueryVo.getStatus()!=null&&cliqueRedQueryVo.getStatus().trim().length()>0&&!cliqueRedQueryVo.getStatus().equals("10")){
					hql.append(" and status ="+cliqueRedQueryVo.getStatus());
				}
				if(cliqueRedQueryVo.getProducts() != null && cliqueRedQueryVo.getProducts().trim().length() > 0){
					hql.append(" and c.products like '%"+cliqueRedQueryVo.getProducts()+"%'");					
				}
			}
	        if(units==null){
				hql.append(" and (status=1 or (exists(select 1 from UserInfo u where u.userId=c.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
			}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
				hql.append(" and createPeople = "+userInfo.getUserId());
			}
	        super.executeUpdate(hql.toString());		
	}

	public void save(CliqueRed cliqueRed) {
		super.save(cliqueRed);
		
	}

	public void examine(String ids, Long status) {
		StringBuffer hql = new StringBuffer("update CliqueRed set status =");
		hql.append(status);
		hql.append(" where cliqueredId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
		
	}

	public void delByIds(String[] ids) {
		super.deleteAllById(CliqueRed.class,ids);
		
	}

	public CliqueRed findById(String id) {
		
		return (CliqueRed)super.findById(CliqueRed.class,id);
	}


	
	public void update(CliqueRed cliqueRed) {
		super.update(cliqueRed);
		
	}


	
	public void changeStatus(List<CliqueRedStatus> cliqueRedStatusList) {
		super.saveAll(cliqueRedStatusList);
		
	}

	public List<CliqueRedStatus> findStatus(String id) {
		
		
		return super.findByProperty(CliqueRedStatus.class,"cliqueredId",id);
	}

	public void deleteById(String id) {
		
		 super.delete(CliqueRed.class, id);
	}


	


	
	
     
}
